import 'package:flutter/material.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/apis.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/express_company_model.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:url_launcher/url_launcher.dart';

/// @description:
/// @author tangzhentao
/// @created at 19:33 2025/3/8


class ExpressCompanyViewmodel extends Viewmodel {
  /// 刷新控制器
  final RefreshController refreshController = RefreshController();
  /// 搜索内容输入框相关
  final TextEditingController keyController = TextEditingController();
  final FocusNode ketNode = FocusNode();

  /// 页码
  int _page = 1;

  /// 数据列表
  final List<Company> list = [];

  /// 搜索
  Future search() async {
    final text = keyController.text;
    if (text.isNotEmpty) {
      loadFirst();
    } else {
      '请输入要搜索的快递公司'.toast();
    }
  }

  /// 加载数据
  Future loadFirst() async {
    setLoading();
    _page = 1;
    final result = await Http().request<ExpressCompanyModel>(Apis.expressCompany, showErrorMsg: false, data: {
      'page': _page, 'expName': keyController.text
    });
    if (result.success) {
      list.clear();
      list.addAll(result.data?.expressBody?.expressList?? []);
      if (list.isNotEmpty) {
        setSuccess();
        refreshController.headerMode?.value = RefreshStatus.canRefresh;
        refreshController.footerMode?.value = list.length < (result.data?.expressBody?.allNum?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      } else {
        setEmpty();
      }
    } else {
      setFailed(msg: result.msg);
    }
  }

  /// 刷新数据
  Future refresh() async {
    _page = 1;
    final result = await Http().request<ExpressCompanyModel>(Apis.expressCompany, showErrorMsg: false, data: {
      'page': _page, 'expName': keyController.text
    });
    if (result.success) {
      refreshController.refreshCompleted();
      refreshController.footerMode?.value = list.length < (result.data?.expressBody?.allNum?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.expressBody?.expressList?? [];
      if (data.isNotEmpty) {
        list.clear();
        list.addAll(data);
        setSuccessChange();
      }
    } else {
      refreshController.refreshFailed();
    }
  }

  /// 加载更多数据
  Future loadMore() async {
    _page++;
    final result = await Http().request<ExpressCompanyModel>(Apis.expressCompany, showErrorMsg: false, data: {
      'page': _page, 'expName': keyController.text
    });
    if (result.success) {
      refreshController.loadComplete();
      refreshController.footerMode?.value = list.length < (result.data?.expressBody?.allNum?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.expressBody?.expressList?? [];
      if (data.isNotEmpty) {
        list.addAll(data);
        setSuccessChange();
      } else {
        refreshController.loadNoData();
      }
    } else {
      refreshController.loadFailed();
    }
  }

  /// 去浏览器打开网页
  void toWebBrowser(int index) async {
    final uri = Uri.parse(list[index].url?? '');
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri);
    }
  }

  @override
  onReload() => loadFirst();

  @override
  onRetry() => loadFirst();

  @override
  void dispose() {
    refreshController.dispose();
    keyController.dispose();
    super.dispose();
  }
}